<?php namespace system\model;
use Hdphp\Model\Model;

class Article extends Model{
    //操作数据表
    protected $table = 'article';
    //自动验证
    protected $validate =[
        //['字段名','验证方法','提示信息',验证条件,验证时间]
         ['title','required','请输入文章标题',3,3],
        ['author','required','请输入文章作者',3,3],
        ['category_cid','required','请选择所属分类',3,3],
        ['thumb','required','请上传缩略图',3,3],
        ['digest','required','请填写文章摘要',3,3],
    ];
    //自动完成
    protected $auto =[
        //['字段名','处理方法','方法类型',验证条件,验证时机]
        ['sendtime','time','function',self::MUST_AUTO,self::MODEL_INSERT],
        ['user_uid','userUid','method',self::MUST_AUTO,self::MODEL_INSERT],
    ];
    //上面自动完成的时候 调用这个方法 自动处理id
    public function userUid(){
        return $_SESSION['admin']['uid'];
    }
    //获取文章的所有数据
    public function getAll($is_recycle){
      $page = Page::row(10)->make($this->where('is_recycle',$is_recycle)->count());
        //
      $data = $this->where('is_recycle',$is_recycle)->limit(page::limit())->orderBy('aid','asc')->get();
       // p($data);exit;
        foreach ($data as $k =>$v){
            $data[$k]['category_cid'] = Db::table('category')->where('cid',$v['category_cid'])->pluck('cname');
        }
       // p($data[$k]['category_cid']);exit;
        return ['page'=>$page,'data'=>$data];
    }
    //添加文章
    public function store(){
      //执行文章表的验证
        if(!$this->create()) return false;
      //文章数据表验
        $articleDataModel = new \system\model\Article_data();
        if(!$articleDataModel->create()) {
            //将article_data里面的错误信息获取到，存入到article表的error属性中
            //这样就可以被文章表article的detError()方法获取到
            $this->error = $articleDataModel->getError();
            return false;
        }
        //验证标签是否有选择
        if(!isset($_POST['tag_tid'])){
            $this->error = '请选择标签';return false;
        }
      //添加文章表article
      //方法1  方法2是上面的自动完成
      // $this->data['sendtime']= time();
       // $this->data['user_uid']= $_SESSION['admin']['uid'];
         $aid = $this->add();
       //文章数据表的添加
        $articleDataModel->data['article_aid'] = $aid;
        $articleDataModel->add();
       //添加文章标签中间表
       $articleTagModel = new \system\model\Article_tag();
        foreach ($_POST['tag_tid'] as $k =>$v){
            $data = [
                'article_aid' => $aid,
                'tag_tid' => $v,
            ];
            $articleTagModel->add($data);
        }
        return true;

    }
    //编辑
    public function edit(){
//        $aid = q('get.aid',0,'intval');
//        p($_POST);exit;
        //执行文章表验证
        if(!$this->create()) return false;
        //验证文章数据article_data
        $articleDataModel = new \system\model\Article_data();
        if(!$articleDataModel->create()){
            $this->error =$articleDataModel->getError();
            return false;
        }
        //执行文章标签中间表验证
        if(!isset($_POST['tag_tid'])){
            $this->error = '请选择文章标签';return false;
        }
        //执行文章表修改
        $this->save();
        //执行文章数据表修改
        $data = [
            'keywords'=>$_POST['keywords'],
            'des'=>$_POST['des'],
            'content'=>$_POST['content'],
        ];
//        p($data);exit;
        $articleDataModel->where('article_aid',$_POST['aid'])->update($data);
        //执行文章标签中间表的修改
        //把当前文章原来的标签删除
        $articleTagModel = new \system\model\Article_tag();
        $articleTagModel->where('article_aid',$_POST['aid'])->delete();
        //执行添加
//        p($_POST['tag_tid']);
//        p($_POST['aid']);die;
        foreach($_POST['tag_tid'] as $k => $v){
            $data = [
                'article_aid'=>$_POST['aid'],
                'tag_tid'=>$v,
             ];
            $articleTagModel->add($data);
        }
        return true;
    }
    //回收站真正删除
    public function del(){
       // p($_POST['aid']);exit;
        //删除文章表
       $this->db->where('aid',$_POST['aid'])->delete();
        //$this->db->where('aid',$_POST['aid'])->delete();
        //删除文章数据表
        $articleDataModel = new \system\model\Article_data();
        $articleDataModel->where("article_aid",$_POST['aid'])->delete();
        //删除文章标签中间表
        $articleTagModel = new \system\model\Article_tag();
        $articleTagModel->where('article_aid',$_POST['aid'])->delect();
        return true;
    }

}
